import time
from fileinput import filename

from airtest.core.api import touch, exists, assert_exists, swipe, wait
from airtest.core.cv import Template
from airtest.core.helper import log

from V1.air import air_directory


class AirClick:
    dir = air_directory.dir

    # 利用基本选择器text进行元素定位
    def airClickByText(self, poco, text):
        try:
            poco(text=text).click()
        except Exception as e:
            print(e)

    # 利用坐标进行元素定位
    def airClickZuoBiao(self, coordinate):
        try:
            touch(coordinate)
        except Exception as e:
            print(e)

    # 利用图片进行元素定位
    def airClickImage(self, filename):
        try:
            touch(
                Template(
                    filename=filename,
                    record_pos=(-0.001, 0.761),
                    resolution=(1080, 2400),
                )
            )
        except Exception as e:
            print(e)

    # 输入
    def airSetText(self, poco, text1, text2):
        try:
            poco(text=text1).set_text(text2)
        except Exception as e:
            print(e)

    # 获取文本
    def airGetText(self, poco, text):
        try:
            poco(text=text).get_text()
        except Exception as e:
            print(e)

    # 空传
    def air_touch_kong(self, poco, kong):
        try:
            poco(kong).click()
        except Exception as e:
            print(e)

    # 图片断言
    def airImageAssert(self, filename, msg):
        try:
            assert_exists(
                Template(
                    filename=self.dir + "//images//" + filename,
                    record_pos=(-0.331, -0.173),
                    resolution=(1080, 2404),
                ),
                msg=msg,
            )
        except Exception as e:
            print(e)

    # 滑动屏幕
    def airSwipScreen(self, v1, v2):
        try:
            swipe(v1=v1, v2=v2)
        except Exception as e:
            print(e)

    # 截图
    def airScreenShot(self, data, title):
        try:
            # 截取一张屏幕截图
            log(data, timestamp=time.time(), desc=title, snapshot=True)
        except Exception as e:
            print(e)

    # 判断图片存在
    def airImageExists(self, filename):
        try:
            if exists(
                    Template(
                        filename=self.dir + "//images//" + filename,
                        record_pos=(-0.117, -0.345),
                        resolution=(1080, 2404),
                    )
            ):
                return True
            else:
                return False
        except Exception as e:
            print(e)

    # 等待图片出现
    def airImageWait(self, filename, time):
        try:
            wait(filename, timeout=time)
        except Exception as e:
            print(e)
